<template>
  <div class="out">
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>库存管理</el-breadcrumb-item>
      <el-breadcrumb-item>出库</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <!-- 搜索和添加 -->
      <el-row type="flex" class="row-bg" justify="space-around">
        <el-col :span="16">
          <el-button icon="el-icon-circle-plus-outline" @click="addOutDialogVisible = true">出库</el-button>
        </el-col>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="outInfo.query" clearable @clear="getOutList">
            <el-button slot="append" icon="el-icon-search" @click="getOutList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 出库列表 -->
      <el-table :data="outList" style="width: 100%" border>
        <el-table-column prop="id" label="编号">
        </el-table-column>
        <el-table-column prop="ycName" label="药材名称">
        </el-table-column>
        <el-table-column prop="outAmount" label="出库数量(克)">
        </el-table-column>
        <el-table-column prop="ycPrice" label="药材单价(元/克)">
        </el-table-column>
        <el-table-column prop="outPrice" label="药材总价(元/克)">
        </el-table-column>
        <el-table-column prop="outDate" label="出库日期">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="warning" icon="el-icon-delete" @click="deleteOut(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="outInfo.pageNum"
        :page-sizes="[4, 6, 8, 9]" :page-size="outInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
    <!-- 出库对话框 -->
    <el-dialog title="出库" :visible.sync="addOutDialogVisible" width="30%" @close="addOutDialogClosed">
      <el-form :model="addOutForm" :rules="addOutFormRules" ref="addOutFormRef" label-width="100px">
        <el-form-item label="药材名称" prop="ycName">
          <el-input v-model="addOutForm.ycName"></el-input>
        </el-form-item>
        <el-form-item label="出库数量" prop="outAmount">
          <el-input v-model="addOutForm.outAmount" type="number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addOutDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOut">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        outInfo: {
          query: '',
          pageNum: 1,
          pageSize: 4

        },
        //页码
        total: 0,
        //出库单数据
        outList: [],
        //控制出库对话框显示
        addOutDialogVisible: false,
        addOutForm: {
          ycName: '',
          outAmount: 0
        },
        //出库对话框验证规则
        addOutFormRules: {
          ycName: [{
            required: true,
            message: '请输入药材名称',
            trigger: 'blur'
          }],
          outAmount: [{
            required: true,
            message: '请输入药材出库数量',
            trigger: 'blur'
          }],
        }
      }
    },
    created() {
      this.getOutList()
    },
    methods: {
      //获取出库单数据
      async getOutList() {
        const {
          data: res
        } = await this.$http.post('/outdata', this.outInfo)
        this.outList = res.data.list
        this.total = res.data.total
      },
      //监听每页条数
      handleSizeChange(size) {
        this.outInfo.pageSize = size
        this.getOutList()
      },
      //监听页码
      handleCurrentChange(page) {
        this.outInfo.pageNum = page
        this.getOutList()
      },
      //
      addOutDialogClosed() {
        this.$refs.addOutFormRef.resetFields()
      },
      //提交出库表单
      addOut() {
        this.$refs.addOutFormRef.validate(async valid => {
          if(!valid) return
          const { data: res } = await this.$http.post('/outdata/add', this.addOutForm)
          console.log(res)
          if (res.status === 'error1') {
            this.addOutDialogVisible = false
            this.$message.error('出库失败，库存不足！')
            return
          }
          if (res.status === 'error2') {
            this.addOutDialogVisible = false
            this.$message.error('出库失败，请先添加药材信息！')
            return
          }
          this.addOutDialogVisible = false
          this.$message.success('出库成功！')
          this.getOutList()
        })
      },
      //删除出库单
      async deleteOut(id) {
         const opRes = await this.$confirm('此操作将永久删除该药材信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if(opRes === 'confirm') {
          const { data: res } = await this.$http.get('/outdel/' + id)
          this.$message.success('删除出库单成功！')
          this.getOutList()
        }
      }
    }
  }
</script>

<style>
</style>
